import * as cc from "cc";
import { C } from "../../utils/ClickableMgr";

const { ccclass, menu, property } = cc._decorator;

/**
 * @desc 点击事件参数
 */
export interface ButtonClickParams{
    /**点击回调 */
    clickListener: (ev: cc.EventTouch) => void, 
    /**是否播放点击动画,默认不播放 */
    anim?: boolean, 
    /**事件绑定目标 */
    target?: any, 
    /**是否检查点击过快,默认不检查 */
    checkClickAble?: boolean
}

@ccclass
@menu("扩展组件/ButtonClick")
export class ButtonClick extends cc.Component {

    private data: ButtonClickParams = null;
    private isMove:boolean = false;
    start() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onClickStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onClickMove, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onClickCancel, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onClickEnd, this);
    }

    onDestroy() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onClickStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onClickMove, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onClickCancel, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onClickEnd, this);
    }

    /**
     * @desc 注册点击事件
     */
    public init(params:ButtonClickParams) {
        this.data = params;
        this.data.target = params.target ? params.target : this;
        this.data.anim = params.anim ? params.anim : false;
        this.data.checkClickAble = params.checkClickAble ? params.checkClickAble : false;        
        this.data.anim && this.addButtonComponent();
    }

    private onClickStart(event: cc.EventTouch) {
        if (!this.data) {
            return;
        }
        this.isMove = false;
        event.propagationStopped = true;
    }
    private onClickMove(event: cc.EventTouch) {
        if (!this.data) {
            return;
        }
        const pos = event.getUILocation();
        const startPos = event.getUIStartLocation();
        const sub = pos.subtract(startPos).length();
        if (sub > 20) {
            this.isMove = true
        }
        event.propagationStopped = true;
    }
    private onClickCancel(event: cc.EventTouch) {
        if (!this.data) {
            return;
        }
        event.propagationStopped = true;
    }
    private onClickEnd(event: cc.EventTouch) {
        if (!this.data) {
            return;
        }
        event.propagationStopped = true;
        this.onClickCancel(event);
        if (!this.isMove) {
            if (this.data.checkClickAble && !C.getT(event).isOk) {
                //按钮点击过快
                return;
            }
            this.data.clickListener.bind(this.data.target)(event)
        }
    }

    addButtonComponent(){
        let button = this.node.getComponent<cc.Button>(cc.Button) || this.node.addComponent<cc.Button>(cc.Button);
        button.transition = cc.Button.Transition.SCALE;
        button.zoomScale = 0.9;
        button.duration = 0.1;
        button.interactable = true;
        
    }
}